'''
Created on 06.04.2012

@author: myaut
'''
import sys

sys.path.append('../../lib')

from salsa.ast import AST
from pycparser import CParser

macros = {
    'FUNCTION_DEF': ('name, t1, a1, t2, a2', 
                     'int name(t1 a1, t2 a2)', None),
    'ASSIGN' : ('a, b', ('a = b'), None),
    'DECLARE': ('name', 'int name;', None),
    'HZ': ('', '1000', None),
    'CODE': ('', 'int i = 0;', None),
    'max': ('t, a, b', 'a > b? (t) a : (t) b', None),
    'ASSERT': ('a, op, b', 'if(a op b) {}', None),
}

test = '''static DECLARE(var);

typedef unsigned u32;

struct {
    int a;
    DECLARE(x);
    int b;
};

int max(int a, int b) {
    return a > b? a :  b;
}

FUNCTION_DEF(name, int, arg1, char, arg2)
{
    DECLARE(z);
    CODE;
    CODE();
    
    while(ASSERT(a, ==, b))
        a += b;
    
    ASSIGN(* (unsigned*) var, arg2);
    
    return max(u32, arg1, HZ);
}
'''

parser = CParser(yacc_debug=True, macros_lookup_func = macros.get)
c_ast = parser.parse(test, filename='test.c', debuglevel=9)

c_ast.show()